/*jslint sloppy: true */
/*global require */
var LM = require('../global');
var APIOrigin = require('../config');
var angular = require('angular');

LM.service('Account', function ($resource) {
	return $resource(APIOrigin + 'manager/user', null, {
		// Action: get from $resource default.
		register: {
			method: "POST"
		},
		alter: {
			method: 'PUT'
		},
		login: {
			method: "POST",
			url: APIOrigin + 'manager/user/login'
		},
		logout: {
			method: 'GET',
			url: APIOrigin + 'manager/user/logout'
		},
		sendActiveCode: {
			method: 'POST',
			url: APIOrigin + 'manager/user/activation'
		},
		isExisted: {
			method: 'GET',
			url: APIOrigin + 'manager/user/:type/:value/existence'
		}
	});
}).service('Case', function ($resource) {
	return $resource(APIOrigin + 'manager/project/:projectId/case/:caseId', {
		projectId: '@projectId'
	}, {
		get: {},
		remove: {
			method: 'DELETE',
			params: {
				caseId: '@caseId'
			}
		},
		create: {
			method: 'POST'
		},
		modify: {
			method: 'PUT',
			params: {
				caseId: '@caseId'
			}
		},
		query: {
			method: 'GET',
			isArray: true
		}
	});
}).service('Project', function ($resource) {
	var Project, pathReg = /^https?:\/\/[0-9a-zA-Z_\.\/%\-]+(:[0-9]{1,5})?(\/[\w\/\.]+)?$/;

	function serialize(obj) {
		var out = '';

		angular.forEach(obj, function (value, key) {
			out += '&' + key + '=' + value;
		});

		return out;
	}

	Project = $resource(APIOrigin + 'manager/project/:projectId', null, {
		get: {
			method: "GET",
			params: {
				projectId: '@projectId'
			}
		},
		queryMine: {
			method: "GET",
			isArray: true
		},
		remove: {
			method: "DELETE",
			params: {
				projectId: '@projectId'
			}
		},
		create: {
			method: 'POST'
		},
		modify: {
			method: 'PUT',
			params: {
				projectId: '@projectId'
			}
		}
	});

	Project.prototype.isProbeValid = function (token) {
		return token && pathReg.test(this.probePath);
	};

	Project.prototype.getUrl = function (isRunner, token, opt) {
		var out = this.probePath + '#/?probe=' +
			this.probeId + '&token=' + token;

		out += isRunner ? '&isRunner' : '';

		out += serialize(opt);

		return out;
	};

	return Project;
}).service('Token', function ($resource, $rootScope) {
	return $resource(APIOrigin + 'manager/project/:projectId/token/:token', {
		projectId: '@projectId'
	}, {
		// Action: get from $resource default.
		remove: {
			method: 'DELETE',
			params: {
				token: '@token'
			}
		},
		create: {
			method: 'POST'
		}
	});
}).service('Result', function ($resource, $rootScope) {
	return $resource(APIOrigin + 'manager/project/:projectId/result/:resultId', null, {
		get: {
			method: 'GET',
			params: {
				resultId: '@resultId'
			}
		},
		remove: {
			method: 'DELETE',
			params: {
				resultId: '@resultId'
			}
		},
		query: {
			method: 'GET',
			isArray: true
		}
	});
}).service('Task', function ($resource, $rootScope) {
	function withoutSchemas(data) {
		delete data.schemas;
		return angular.toJson(data);
	}

	return $resource(APIOrigin + 'manager/project/:projectId/task/:taskId', {
		projectId: '@projectId'
	}, {
		// Action: get from $resource default.
		remove: {
			method: 'DELETE',
			params: {
				taskId: '@taskId'
			}
		},
		modify: {
			method: 'PUT',
			params: {
				taskId: '@taskId'
			},
			transformRequest: withoutSchemas
		},
		query: {
			method: 'GET',
			isArray: true
		}
	});
});
